﻿using PoemGameWebApiService.Shared;
using System.Text.Json;

namespace PoemGame.Domain.Services.Factory.WebApi
{
    public class CheckAnswerServiceRemote:ICheckAnswerService
    {
        private readonly HttpClient _httpClient;
        private readonly string url;

        public CheckAnswerServiceRemote(HttpClient httpClient, string url)
        {
            _httpClient = httpClient;
            this.url = url;
        }

        public async Task<bool> CheckAnswer(CheckAnswerServiceInput input, string answer)
        {
            var dto = new CheckAnswerServiceDto() { Input = input,Answer = answer};
            var str = JsonSerializer.Serialize(dto);
            var content = new StringContent(str);
            content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("application/json");

            var response = await _httpClient.PostAsync(url, content);
            response.EnsureSuccessStatusCode();
            var res = await response.Content.ReadAsStringAsync();
            return bool.Parse(res);
        }
    }
}
